from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Table
from sqlalchemy.orm.session import Session

from airflow.models.base import Base
from airflow.utils import dates
from airflow.utils.session import provide_session


class AuthUserRole(Base):
    """the class to get user info."""

    __tablename__ = "idata_auth_user_role"
    __table_args__ = {"useexisting": True}

    id = Column(Integer, primary_key=True, autoincrement=True)
    role_id = Column(Integer)
    oa_user_name = Column(String(32))
    create_time = Column(DateTime)

    def __init__(self, role_id, oa_user_name, create_time):
        self.role_id = role_id
        self.oa_user_name = oa_user_name
        self.create_time = create_time

    @staticmethod
    @provide_session
    def get_user_role(id, session: Session = None):
        return session.query(AuthUserRole).filter(AuthUserRole.id == id).first()

    @staticmethod
    @provide_session
    def get_user_roles(session: Session = None):
        return session.query(AuthUserRole).all()

    @staticmethod
    @provide_session
    def get_user_roles_page(page, session: Session = None):
        return session.query(AuthUserRole).order_by(AuthUserRole.create_time).paginate(page, 10, error_out=False)

    @staticmethod
    @provide_session
    def get_user_role_by_oausername(oa_user_name, session: Session = None):
        """
        get_user_role_by_oausername.

        :param id: The role_resource id of the roleResource to get.
        :param session: SQLAlchemy ORM Session
        """
        return session.query(AuthUserRole).filter(AuthUserRole.oa_user_name == oa_user_name)

    @staticmethod
    @provide_session
    def add_user_role(user_role, session: Session = None):
        return session.add(user_role)

    @staticmethod
    @provide_session
    def delete_user_role(id, session: Session = None):
        """
        delete user role  by id .

        :param id: The role_resource id of the roleResource to get.
        :param session: SQLAlchemy ORM Session
        """
        session.query(AuthUserRole).filter(AuthUserRole.id == id).delete()

    @staticmethod
    @provide_session
    def delete_user_role_by_oausername(oa_user_name, session: Session = None):
        """
        delete user role  by id .

        :param id: The role_resource id of the roleResource to get.
        :param session: SQLAlchemy ORM Session
        """
        session.query(AuthUserRole).filter(AuthUserRole.oa_user_name == oa_user_name).delete()

    @staticmethod
    @provide_session
    def update_user_role(user_role, session: Session = None):
        result = session.query(AuthUserRole).filter(AuthUserRole.id == user_role.id).first()
        if result is None:
            return
        else:
            return session.merge(user_role)

    def to_json(self):
        if self is None:
            return None
        data = {
            'id': self.id,
            'role_id': self.role_id,
            'oa_user_name': self.oa_user_name,
            'create_time': dates.to_milliseconds(self.create_time),
            'enable': self.enable
        }
        return data






